package com.zhcd.sys.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhcd.sys.pojo.Doctor;
import com.zhcd.sys.service.DoctorService;
import com.zhcd.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author feifei
 * @since 2021-11-29
 */
@Controller
@RequestMapping("/Doctor/")
public class DoctorController {
    @Autowired
    public DoctorService doctorService;

    //查询全部医生
    @RequestMapping(value = "/getAll_Doctor",method = RequestMethod.GET)
    @ResponseBody
    public String getAll_Doctor() throws JsonProcessingException {
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("code","200");
        hs.put("data",doctorService.getAll_Doctor());
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    //按条件(电话、姓名、科室)查询医生
    @RequestMapping(value = "/getList_Doctor",method = RequestMethod.GET)
    @ResponseBody
    public String getList_Doctor(@RequestBody Map<String,Object> para) throws JsonProcessingException{
        String doctorphone=(String)para.get("doctorphone");
        String doctorname=(String)para.get("doctorname");
        String departmentname=(String)para.get("departmentname");
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("code","200");
        hs.put("data",doctorService.getList_Doctor(doctorphone,doctorname,departmentname));
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    //查询单个医生
    @RequestMapping(value = "/getOne_Doctor",method = RequestMethod.GET)
    @ResponseBody
    public String getOne_Doctor(@RequestBody Map<String,Object> para) throws JsonProcessingException{
        Integer doctorid=(Integer)para.get("doctorid");
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("code","200");
        hs.put("data",doctorService.getOne_Doctor(doctorid));
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    //删除医生
    @RequestMapping(value = "/del_Doctor",method = RequestMethod.DELETE)
    @ResponseBody
    public String del_Doctor(@RequestBody Map<String,Object> para) throws JsonProcessingException{
        Integer doctorid=(Integer)para.get("doctorid");
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("code","200");
        hs.put("data",doctorService.del_Doctor(doctorid));
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    //修改医生
    @RequestMapping(value = "/upd_Doctor",method = RequestMethod.POST)
    @ResponseBody
    public String upd_Doctor(@RequestBody Map<String,Object> para) throws JsonProcessingException{
        Doctor doctor=(Doctor)para.get("doctor");
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("code","200");
        hs.put("data",doctorService.upd_Doctor(doctor));
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }



    /*
     * @RequestMapping：为当前方法配置访问路径
     * 如果Controllor类上没有配置访问路径，当前项目中所有controller中方法上的访问路径都不能冲突
     */
    @RequestMapping("Index")
    public String Index(Model model) {
        List<Doctor> lists = doctorService.selectAll();
        model.addAttribute("list", lists);
        return "/Views/monitor/doctor/sel_doctor";
    }


    @RequestMapping("Edit")
    public String Edit(String id,Model model) {

        Doctor obj = new Doctor();
        if(id == null ||"".equals(id))
        {

        }
        else{
            obj = doctorService.simpleObj(id);
        }
        model.addAttribute("obj", obj);
        return "/Views/monitor/doctor/add_doctor";
    }


    @RequestMapping(value = "Edit2",method = RequestMethod.POST)
    @ResponseBody
    public String Edit2(@RequestBody Map<String,Object> data){
        JsonResult result=new JsonResult();

        Doctor model=new Doctor();
        var id=data.get("id").toString();
        model.setDoctorid(data.get("id").toString());
        model.setDepartmentid("");
        model.setDepartmentname(data.get("name").toString());
        model.setDoctorphone(data.get("phone").toString());
        model.setDoctoridcard(data.get("card").toString());
        model.setDoctorage(Integer.parseInt(data.get("age").toString()));
        model.setDoctorsex(Integer.parseInt(data.get("sex").toString()));
        model.setAddress(data.get("address").toString());
        model.setCreatetime(new Date());
        model.setHospitalid("");
        model.setHospitalname("");

        try {
            doctorService.Add(model);
            if(id.equals("")) //添加
            {
                result.IsOK("添加成功");
            }
            else
            {
                result.IsOK("修改成功");
            }
        }
        catch (Exception exception)
        {
            String msg="数据失败,原因:"+exception.toString();
            result.IsError(msg);
        }
        //.ToJson()
        System.out.println(result.ToJson());
        return  result.ToJson();
    }



}

